package com.expert.conrtoller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.expert.annotations.RoleAnnotation;
import com.expert.dto.EditCategoryDTO;
import com.expert.enums.RoleLevel;
import com.expert.pojo.Category;
import com.expert.result.Result;
import com.expert.service.CategoryService;
import com.expert.vo.CategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: CategoryController
 * @Author: cxs
 * @Date: 2024/03/12 14:40
 * @Description: 分类的controller
 **/
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {


    @Autowired
    CategoryService categoryService;

    /**
     * 分页查询专家分类信息
     *
     * @param page          当前页
     * @param pageSize      每页条数
     * @param categoryCode  分类代码
     * @param categoryName  分类名
     * @param categoryLevel 分类级别
     * @return categoryVo 封装的分类信息
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @GetMapping("/page")
    public Result<Page<CategoryVo>> pageResult(int page, int pageSize, String categoryCode,
                                               String categoryName, Integer categoryLevel,Long categoryParent) {
        log.info("分页查询分类信息");
        Page<CategoryVo> categoryPage = this.categoryService.pageCategory(page, pageSize, categoryCode, categoryName, categoryLevel,categoryParent);
        return Result.success(categoryPage);
    }

    /**
     * 根据分类级别查询对于的父级分类信息
     * @param level 父级分类级别
     * @return 父级分类信息
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @GetMapping("/parent/{level}")
    public  Result<List<Category>> getCategoryParent(@PathVariable("level")String level ){
        log.info("根据分类级别查询父级分类");
        List<Category> categoryParentList=this.categoryService.getCategoryParent(Integer.parseInt(level));
        return  Result.success(categoryParentList);
    }
    /**
     * 根据分类级别查询对于的父级分类信息
     * @param level 父级分类级别
     * @return 父级分类信息
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @GetMapping("/{parent}/{level}")
    public  Result<List<Category>> getCategoryByLevelAndParentId(@PathVariable("parent") Long parent,@PathVariable("level")Integer level ){
        log.info("根据父类和级别查询信息");
        List<Category> categoryList=this.categoryService.getCategoryByParentAndLevel(parent,level);
        return  Result.success(categoryList);
    }
    /**
     * 根据分类级别查询对于的父级分类信息
     * @return 父级分类信息
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @GetMapping("/parent")
    public  Result<List<Category>> getAllCategoryParent(){
        log.info("根据分类级别查询父级分类");
        List<Category> categoryParentList=this.categoryService.getCategoryAllParent();
        return  Result.success(categoryParentList);
    }

    /**
     * 添加分类信息
     * @param category 前端传递的分类参数
     * @return success
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody Category category){
        log.info("添加分类信息");
        this.categoryService.addCategory(category);
        return Result.success();
    }

    /**
     * 删除分类信息
     * @param ids 待删除id
     * @return success
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @DeleteMapping("/delete")
    public Result deleteCategory(@RequestParam List<Long> ids){
        log.info("删除分类信息");
        this.categoryService.deleteCategory(ids);
        return Result.success();
    }

    /**
     * 根据id查询分类信息
     * @param id 分类id
     * @return 查询结果
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @GetMapping("/{id}")
    public Result<Category> getCategoryById(@PathVariable("id") Long id){
        log.info("根据id查询分类信息");
        Category category= this.categoryService.selectById(id);
        return Result.success(category);
    }
    /**
     * 根据name查询分类信息
     * @param name 分类name
     * @return 查询结果
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @GetMapping("/getByName")
    public Result<List<Category>> getCategoryById(@RequestParam String name){
        log.info("根据分类名查询分类信息");
        List<Category> categoryList= this.categoryService.selectByName(name);
        return Result.success(categoryList);
    }
    /**
     * 修改分类信息
     * @param editCategoryDTO 前端传递的修改的分类信息
     * @return success
     */
    @RoleAnnotation(level = RoleLevel.SUPER)
    @PutMapping("/updateCategory")
    public  Result updateCategory(@RequestBody EditCategoryDTO editCategoryDTO){
        log.info("修改分类信息：{}",editCategoryDTO);
        this.categoryService.updateCategory(editCategoryDTO);
        return  Result.success();
    }


    @RoleAnnotation(level = RoleLevel.SUPER)
    @GetMapping("/all")
    public Result<List<Category>> getAllCategory(){
        log.info("查询所有分类信息");
        return Result.success(this.categoryService.list());
    }
}
